package com.apping.ser.app;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.apping.ser.app.service.BindService;

/**
 * Created by baisu on 9/2/2014.
 */
public class BindServiceTest extends Activity {
    private Button bind;
    private Button unBind;
    private Button getServiceStatus;
    BindService.MyBinder binder;

    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            System.out.println("--Service Connected--");
            binder = (BindService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            System.out.println("--Service Disconnected--");
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initUI();
    }

    public void initUI() {
        bind = (Button)findViewById(R.id.bind);
        unBind = (Button)findViewById(R.id.unbind);
        getServiceStatus = (Button)findViewById(R.id.getSserviceStatus);

        final Intent intent = new Intent();
        intent.setAction("com.apping.ser.app.service.BIND_SERVICE");
        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bindService(intent, conn, Service.BIND_AUTO_CREATE);
            }
        });

        unBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(conn);
            }
        });

        getServiceStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(BindServiceTest.this,
                        "service的count为： " + binder.getCount(),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}
